package com.yoo.ons.adapter

import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.yoo.ons.model.HomeListModel
import com.makeramen.roundedimageview.RoundedImageView
import com.yoo.ons.R
import com.yoo.ons.util.loadThumbnails
import com.youth.banner.adapter.BannerAdapter

class HomeImageAdapter(
    val context: Context,
    val data: List<HomeListModel.PhotoBean>,
) : BannerAdapter<HomeListModel.PhotoBean, HomeImageAdapter.BannerViewHolder>(data) {
    val load = arrayListOf<Int>()
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): BannerViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_home_image,null)
        //注意，必须设置为match_parent，这个是viewpager2强制要求的
        view.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        return BannerViewHolder(view)
    }

    override fun onBindView(holder: BannerViewHolder?, bean: HomeListModel.PhotoBean, position: Int, size: Int) {
        holder?.apply {
            val iv = view.findViewById<RoundedImageView>(R.id.item_home_image)
//            val imageLoading = view.findViewById<LottieAnimationView>(R.id.image_loading)
//            imageLoading.visibility = View.GONE
//            if (load.indexOf(position) == -1) {
//                imageLoading.visibility = View.VISIBLE
//                imageLoading.playAnimation()
//                iv.loadImage(context, bean.imageUrl, position) {
//                    imageLoading.visibility = View.GONE
//                    imageLoading.cancelAnimation()
//                    load.add(position)
//                }
//            } else {
            Log.i("HomeImageAdapter","${bean.imageUrl}")
            iv.loadThumbnails(bean.imageUrl,iv.width)

//            Glide.with(context)
//                    .load(bean.imageUrl)
//                    .dontAnimate()
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
//                    .into(iv)
//            }

        }
    }

    inner class BannerViewHolder(var view: View) : RecyclerView.ViewHolder(
        view
    )
}